package com.ruoyi.riskrule.component;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.bizsys.domain.CreditApply;
import com.ruoyi.bizsys.domain.CustReplenishInfo;
import com.ruoyi.bizsys.domain.RuleIn;
import com.ruoyi.bizsys.service.ICreditAccountService;
import com.ruoyi.bizsys.service.ICreditApplyService;
import com.ruoyi.bizsys.service.ICustReplenishInfoService;
import com.ruoyi.bizsys.service.IRuleInService;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.extdata.common.config.RuleConfig;
import com.ruoyi.riskrule.config.BindCardParamsLoader;
import com.ruoyi.riskrule.config.Step1ParamsLoader;
import com.ruoyi.riskrule.config.common.RuleUtils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@AllArgsConstructor
public class RaiseLimitComponent {

    private Step1ParamsLoader step1ParamsLoader;

    private ICreditAccountService creditAccountService;

    private IRuleInService ruleInService;

    private BindCardParamsLoader bindCardParamsLoader;

    private ICustReplenishInfoService replenishInfoService;

    public JSONObject paramsAssembly(String ruleType, String custNo) {
        RuleIn ruleIn = null;
        //信息补充提额
        if (RuleConfig.RULE_TYPE_REPLENISH.equals(ruleType)) {
            ruleIn = replenishAssembly(custNo);
        }
        //绑卡
        if (RuleConfig.RULE_TYPE_BIND_CARD.equals(ruleType)) {
            ruleIn = bindCardParamsLoader.initRuleInParam(custNo);
        }
        ruleInService.insertRuleIn(ruleIn);
        return RuleUtils.riskParamConversion(ruleIn);
    }

    private RuleIn replenishAssembly(String custNo) {
        CustReplenishInfo replenishInfo = replenishInfoService.getOneByCustNo(custNo);
        RuleIn ruleIn = step1ParamsLoader.initRuleInParam(RuleConfig.RULE_TYPE_REPLENISH, replenishInfo.getSerialNo());
        ruleIn.setAdjustSerialNo(replenishInfo.getSerialNo());
        ruleIn.setCreditLine(creditAccountService.getLatestQuota(custNo).toString());
        //工作流编号
        ruleIn.setWorkflowCode(RuleConfig.RULE_WORK_FLOW_CODE_LIMIT_ACTIVE_LIMIT);
        ruleIn.setOperType("7");
        return ruleIn;
    }


}
